package com.he.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.gson.Gson;
import com.qiniu.http.Client;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/13 21:24
 * @Desc 七牛云OSS配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "oss")
public class OssProperties {
    /**
     * oss域名
     */
    private String url;

    /**
     * 区域
     */
    private String region;

    /**
     * 访问密钥id
     */
    private String accessKey;

    /**
     * 访问密钥密码
     */
    private String secretKey;

    /**
     * bucket名称
     */
    private String bucketName;

    /**
     * 从配置文件中读取区域（region）并配置
     */
    @Bean
    public com.qiniu.storage.Configuration qiniuConfig() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Field[] fields = QiNiuRegion.class.getDeclaredFields();
        for(Field field:fields){
            String r = (String)field.get(QiNiuRegion.class);
            System.out.println(r);
            if(r.equals(region)){
                com.qiniu.storage.Configuration c = new com.qiniu.storage.Configuration(
                        (Region)Region.class
                                .getMethod(r,null)
                                .invoke(null,null));
                return c;

            }
        }
        return new com.qiniu.storage.Configuration(Region.autoRegion());
    }

    /**
     * 构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager(com.qiniu.storage.Configuration c) {
        return new UploadManager(c);
    }


    /**
     * 认证信息实例
     * @return
     */
    @Bean
    public Auth auth() {
        return Auth.create(accessKey, secretKey);
    }

    /**
     *  构建七牛空间管理实例
     * @param c com.qiniu.storage.Configuration
     * @return
     */
    @Bean
    public Client client(com.qiniu.storage.Configuration c){
        return new Client(c);
    }

    /**
     * 构建七牛空间管理实例
     * @param auth 认证信息
     * @param c com.qiniu.storage.Configuration
     * @return
     */
    @Bean
    public BucketManager bucketManager(Auth auth,com.qiniu.storage.Configuration c) {
        return new BucketManager(auth, c);
    }

    /**
     * Gson
     * @return
     */
    @Bean
    public Gson gson() {
        return new Gson();
    }

}
interface QiNiuRegion{
    String REGION_0 = "region0";
    String REGION_1 = "region1";
    String REGION_2 = "region2";
    String REGION_Na = "regionNa0";
    String REGION_AS = "regionAs0";
    String HUA_DONG = "huadong";
    String HUA_BEI = "huabei";
    String HUA_NAN = "huanan";
    String BRI_MEI = "beimei";
    String XIN_JIA_PO = "xinijipo";
}

